package ocr.utils;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Interpolation {

	public static String DEFAULT_FILENAME="new.jpg";
	public static String DEFAULT_FILETYPE="jpg";
	/*
	 * 
	 * @param pixels Image pixels.
	 * 
	 * @param w Image width.
	 * 
	 * @param h Image height.
	 * 
	 * @param w2 New width.
	 * 
	 * @param h2 New height.
	 * 
	 * @return New array with size w2 * h2.
	 */
	public static int[] resizeBilinearGray(int[] pixels, int w, int h, int w2,
			int h2) {
		int[] temp = new int[w2 * h2];
		int A, B, C, D, x, y, index, gray;
		float x_ratio = ((float) (w - 1)) / w2;
		float y_ratio = ((float) (h - 1)) / h2;
		float x_diff, y_diff, ya, yb;
		int offset = 0;
		for (int i = 0; i < h2; i++) {
			for (int j = 0; j < w2; j++) {
				x = (int) (x_ratio * j);
				y = (int) (y_ratio * i);
				x_diff = (x_ratio * j) - x;
				y_diff = (y_ratio * i) - y;
				index = y * w + x;

				// range is 0 to 255 thus bitwise AND with 0xff
				A = pixels[index] & 0xff;
				B = pixels[index + 1] & 0xff;
				C = pixels[index + w] & 0xff;
				D = pixels[index + w + 1] & 0xff;

				// Y = A(1-w)(1-h) + B(w)(1-h) + C(h)(1-w) + Dwh
				gray = (int) (A * (1 - x_diff) * (1 - y_diff) + B * (x_diff)
						* (1 - y_diff) + C * (y_diff) * (1 - x_diff) + D
						* (x_diff * y_diff));

				temp[offset++] = gray;
			}
		}
		return temp;
	}

	/**
	 * Bilinear resize ARGB image. pixels is an array of size w * h. Target
	 * dimension is w2 * h2. w2 * h2 cannot be zero.
	 * 
	 * @param pixels
	 *            Image pixels.
	 * @param w
	 *            Image width.
	 * @param h
	 *            Image height.
	 * @param w2
	 *            New width.
	 * @param h2
	 *            New height.
	 * @return New array with size w2 * h2.
	 */
	public static int[] resamplingBilinear(int[] pixels, int w, int h, int w2,
			int h2) {
		int[] temp = new int[w2 * h2];
		int a, b, c, d, x, y, index;
		float x_ratio = ((float) (w - 1)) / w2;
		float y_ratio = ((float) (h - 1)) / h2;
		float x_diff, y_diff, blue, red, green;
		int offset = 0;
		for (int i = 0; i < h2; i++) {
			for (int j = 0; j < w2; j++) {
				x = (int) (x_ratio * j);
				y = (int) (y_ratio * i);
				x_diff = (x_ratio * j) - x;
				y_diff = (y_ratio * i) - y;
				index = (y * w + x);
				a = pixels[index];
				b = pixels[index + 1];
				c = pixels[index + w];
				d = pixels[index + w + 1];

				// blue element
				// Yb = Ab(1-w)(1-h) + Bb(w)(1-h) + Cb(h)(1-w) + Db(wh)
				blue = (a & 0xff) * (1 - x_diff) * (1 - y_diff) + (b & 0xff)
						* (x_diff) * (1 - y_diff) + (c & 0xff) * (y_diff)
						* (1 - x_diff) + (d & 0xff) * (x_diff * y_diff);

				// green element
				// Yg = Ag(1-w)(1-h) + Bg(w)(1-h) + Cg(h)(1-w) + Dg(wh)
				green = ((a >> 8) & 0xff) * (1 - x_diff) * (1 - y_diff)
						+ ((b >> 8) & 0xff) * (x_diff) * (1 - y_diff)
						+ ((c >> 8) & 0xff) * (y_diff) * (1 - x_diff)
						+ ((d >> 8) & 0xff) * (x_diff * y_diff);

				// red element
				// Yr = Ar(1-w)(1-h) + Br(w)(1-h) + Cr(h)(1-w) + Dr(wh)
				red = ((a >> 16) & 0xff) * (1 - x_diff) * (1 - y_diff)
						+ ((b >> 16) & 0xff) * (x_diff) * (1 - y_diff)
						+ ((c >> 16) & 0xff) * (y_diff) * (1 - x_diff)
						+ ((d >> 16) & 0xff) * (x_diff * y_diff);

				temp[offset++] = 0xff000000
						| // hardcode alpha
						((((int) red) << 16) & 0xff0000)
						| ((((int) green) << 8) & 0xff00) | ((int) blue);
			}
		}
		return temp;
	}

	/**
	 * save bufferedimage to file
	 * @param img
	 * @param filename
	 * @param filetype
	 * @return
	 */
	public static File saveImage(BufferedImage img, String filename,
			String filetype) {

		File file = new File(filename);

		try {
			ImageIO.write(img, filetype, file);
			return file;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}
	
	/**
	 * bilinear interpolation
	 * @param sourceimg: source image
	 * @param rate : 
	 * @return
	 */
	public static File interpolation(File sourceimg,int rate){
		BufferedImage imagebuf;
		try {
			imagebuf = ImageIO.read(sourceimg);
			int w = imagebuf.getWidth();
			int h = imagebuf.getHeight();
			int pixels[] = new int[w * h];
			imagebuf.getRGB(0, 0, w, h, pixels, 0, w);

			int newpixels[] = resamplingBilinear(pixels, w, h, w * rate,
					h * rate);
			BufferedImage newimg = new BufferedImage(w*rate, h*rate, BufferedImage.TYPE_3BYTE_BGR);
			newimg.setRGB(0, 0, w*rate, h*rate, newpixels, 0, w*rate);
			newimg.flush();
			
			//String filename =DEFAULT_FILENAME;
			//String filetype =DEFAULT_FILETYPE;
			File file = saveImage(newimg, DEFAULT_FILENAME, DEFAULT_FILETYPE);
			return file;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	/*
	public static void writeResult(String str){
		File file = new File("result.txt");
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(file));
			writer.write(str);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/
	public static void main(String[] args) {
		File file = new File("vietsample.tif");
		//file = interpolation(file, 2);
		String result = Ocr.doOCR(file, "eng");
		System.out.println(result);
		//writeResult(result);
	}
}
